<?php
/**
 * 考勤报表
 * 如果申请的是27-30的假,然后在29号的时候撤消的假
 * 如果是24号申请25-28的假期 27号的时候还没有批 到28号批了
 *  7.25 7.28号假
7.25申请 7.29号的假,不管需要在job里直接扣除
7.25号 申请 8.5号假
7.25号 请7.30-8.5号 不管
7.25号 请6.29 管

29号申请 27号的假 管
29号请30假 管
29号 请 30-8.2号 管
29号 8.5 不管
8.5申请 7.28号的假 管
8.5号 7.29号 管
6.5号 6.18-10.26 不管
*/
apf_require_class("Bll_BLL");
apf_require_class("Bll_Attendance_UserInfo");
apf_require_class("Tools_OAUrls");
apf_require_class('Bll_Attendance_AccessDayRecord');
apf_require_class('Bll_Attendance_DailyLeave');
apf_require_class('Bll_Attendance_LeaveRecord');
apf_require_class('Bll_Attendance_MonthSpecialSet');

class Bll_Attendance_DailyReport extends BLL{

    private $kq_start_time=20120701;//打卡记录只是从这一天开始。之前的请假记录被忽略
    public function __construct(){
        $this->daily_bll = new Bll_Attendance_DailyLeave();
        $this->leave_bll = new Bll_Attendance_LeaveRecord();
    }
    /**
     * 员工的月份的数据报表数据
     * @param int $uid
     * @param int $yearmonth 201208
     */
    public function build_report_data($uid,$params){
        //公出和出差时需要计算上月未清数据



    }

    /**
     * 员工起始时间的考勤明细
     * @param int $uid
     * @param int $start 20120801
     * @param int $end 20120831
     */
    public function get_daily_detail($uid,$start,$end){

    }
    /**
     * 提交一个申请，折分存到指定的表
     * @param int $leave_id
     * @param int $status
     * @return boolean
     */
    public function make_leave_data($leave_id,$status){
        $leave_data=$this->get_leave_record_by_id($leave_id);

        switch ($leave_data['type']){
            case Const_LeaveType::LEAVE_TYPE_CHIDAO:
                if($leave_data['leave_reason'] == '地铁故障'){#直接通过走vb
                    $status=100;
                }
                $this->update_come_late($leave_data,$status);
            break;
            case Const_LeaveType::LEAVE_TYPE_ZAOTUI:
                $this->update_left_early($leave_data,$status);
            break;
            case Const_LeaveType::LEAVE_TYPE_GONGCHU:
                $this->update_business_travel($leave_data,$status);
            break;
            case Const_LeaveType::LEAVE_TYPE_CHUCHA:
                $this->update_business_trip($leave_data, $status);
            break;
            default:
                $this->update_common_leave($leave_data,$status);
                break;
        }
        return true;
    }
    /**
     * 获得所有员工今天还有效的请假记录
     */
    public function get_user_leave_record(){

    }

    public function get_leave_record_by_id($leave_id){
        $leave_data=$this->leave_bll->get_leave_by_id($leave_id);
        $leave_data['start_time'] = strtotime($leave_data['start_time']);
        $leave_data['end_time'] = strtotime($leave_data['end_time']);
        $leave_data['type'] = $this->format_leave_type($leave_data['type']);
        $leave_data['status']=$this->format_leave_status($leave_data['status']);
        $leave_data['daylist']=$this->format_leave_days($leave_data['start_time'],$leave_data['end_time'],$leave_data['type']);
        $user_bll = new Bll_Attendance_UserInfo();
        $sp_user_list = $user_bll->get_all_special_work_user();
        $leave_data['hm_date']=array();//这个员工是否为特殊班别
        if(!empty($sp_user_list)){
            if(isset($sp_user_list[$leave_data['user_id']])){
                $leave_data['hm_date'] =$sp_user_list[$leave_data['user_id']];
            }
        }
        return $leave_data;
    }

    /**
     * 分割一次请假中那些日期
     * 1-结算日
     * 结算日到下月1号
     * 需要传入结算日当为标识,默认使用当前月份
     */
    public function split_leave_days($leave_data){
        //如果外围已经格式化好数据,这几个就可省略
        if(!is_numeric($leave_data['start_time'])){
            $leave_data['start_time'] = strtotime($leave_data['start_time']);
            $leave_data['end_time'] = strtotime($leave_data['end_time']);
            $leave_data['type'] = $this->format_leave_type($leave_data['type']);
            $leave_data['status']=$this->format_leave_status($leave_data['status']);
        }

        $apply_day = date('Ymd',$leave_data['apply_time_ext']);//20120725;
        $leave_type = APF::get_instance()->get_config('daily_status');//TODO查看type中有没有出差
        $month_bll = new Bll_Attendance_MonthSpecialSet();
        $result = array();
        $daylist = $this->format_leave_days($leave_data['start_time'],$leave_data['end_time'],$leave_data['type']);
        if(in_array($leave_data['type'],$leave_type)){
            $result['curr'] = $daylist;
        }else{
            foreach($daylist as $k=> $val){
                //根据当天取当月的结算日
                $month_set = $month_bll->get_month_special_set(date('Ym',strtotime($k)));
                $day = $month_set['settlement'];
                $markup = $this->time_markup($day);#获取结算上下月份的时间标记

                if($apply_day <=$day){//小于结算日
                    if(($k>=$markup['curr_month_start_day'] && $k<=$markup['curr_month_end_day']) || $k>=$markup['next_month_start_day']){
                        $result['next'][$k]=$val;
                    }else{
                        $result['prev'][$k]=$val;
                    }
                }else{
                    if($k<=$markup['curr_month_end_day']){#7.25号 请6.29
                        $result['prev'][$k]=$val;
                    }else{
                        $result['next'][$k]=$val;
                    }
                }
            }
        }
        return $result;
    }
    /**
     *撤消假，需要对多次请的作清理
     *@param int $uid
     *@param int $day,那些天需要重新检测
     *@param array $status
     **/
    function back_kq_status($uid,$day,$status){
        $today =date('Ymd');
        $updata = array(
             'am_status'=>$status[0],
             'am_type'=>0,
             'am_leave_status'=>0,
             'pm_status'=>$status[1],
             'pm_type'=>0,
             'pm_leave_status'=>0,
         );
         if($day>$today){
             $updata['am_status']=5;
             $update['pm_status']=6;
             return $updata;
         }
         $allow_status = array(Const_LeaveType::LEAVE_STATUS_AUDIT,Const_LeaveType::LEAVE_STATUS_PASS);
         $curr_data = $this->daily_bll->find_user_leave_daily_by_dateline($uid,$day);
         if(!empty($curr_data)){
             foreach($curr_data as $val){
                 if($val['leave_type']==Const_LeaveType::LEAVE_TYPE_CHUCHA && in_array($val['leave_status'],$allow_status)){
                     if($val['leave_apm']==1){
                         $updata['am_type']=Const_LeaveType::LEAVE_TYPE_CHUCHA;
                         $updata['am_leave_status']=$val['leave_status'];
                     }elseif($val['leave_apm']==2){
                         $updata['pm_type']=Const_LeaveType::LEAVE_TYPE_CHUCHA;
                         $updata['pm_leave_status']=$val['leave_status'];
                     }else{
                         $updata['am_type']=$updata['pm_type']=Const_LeaveType::LEAVE_TYPE_CHUCHA;
                         $updata['am_leave_status']=$updata['pm_leave_status']=$val['leave_status'];
                     }
                 }
             }
        }

        if($updata['am_type'] && $updata['pm_type']){
            continue;
        }
        $ext_data = $this->daily_bll->find_user_leave_ext_by_dateline($uid,$day);
            if(!empty($ext_data)){
                 foreach($ext_data as $val){
                     if(in_array($val['am_leave_status'],$allow_status) || in_array($val['pm_leave_status'],$allow_status)){
                         if($val['am_type'] && empty($updata['am_type'])){
                             $updata['am_type']=$val['am_type'];
                             $updata['am_leave_status']=$val['am_leave_status'];
                         }
                         if($val['pm_type'] && empty($updata['pm_type'])){
                             $updata['pm_type']=$val['pm_type'];
                             $updata['pm_leave_status']=$val['pm_leave_status'];
                         }
                 }
            }
         }
         return $updata;
    }
    /**
     * 申请状态已撤消
     * @param int $leave_id
     * @param int $reject
     * @return boolean
     */
    public function audit_cancel_leave_record($leave_id){
        $leave_data=$this->leave_bll->get_leave_by_id($leave_id);
        $daylist = $this->format_leave_days($leave_data['start_time'],$leave_data['end_time'],$leave_data['type']);
        $status = Const_LeaveType::LEAVE_STATUS_CANCEL;
        $am_where['am_leave_id'] = $leave_id;
        $pm_where['pm_leave_id'] = $leave_id;
        //这里分两次分别对上午的状态进行改变
        $this->daily_bll->update_record_next(array('am_leave_status'=>$status),$am_where);
        $this->daily_bll->update_record_next(array('pm_leave_status'=>$status),$pm_where);

        $this->daily_bll->update_record_curr(array('leave_status'=>$status),array('leave_id'=>$leave_id));
        //上月未清表
        $this->daily_bll->update_record_prev(array('leave_status'=>$status),array('leave_id'=>$leave_id));
        //开始为每天的假期修改状态，最后通过打卡记录重设上下午状态,还清理请假记录
        foreach($daylist as $k=>$val){
            $oneday=$this->daily_bll->find_kq_by_dateline($leave_data['user_id'],$k);
            if(!empty($oneday)){
                $status = $this->get_one_accessday($leave_data['user_id'],$k);
                $up=array();
                if($leave_data['type']==Const_LeaveType::LEAVE_TYPE_CHIDAO){
                    $up['am_status']=$status['status'][0];
                    $up['is_later']=1;
                }elseif ($leave_data['type']==Const_LeaveType::LEAVE_TYPE_ZAOTUI){
                    $up['pm_status']=$status['status'][1];
                    if($status[1]==2){
                        $up['leave_early']=1;
                    }
                }else{
                    $up = $this->back_kq_status($leave_data['user_id'],$k,$status['status']);
                    /*
                    $up = array(#是公出不用管day字段
                        'days'=>$oneday['days']-$val['am']-$val['pm'],
                    );
                    if($val['am']){//对上下午的申请假进行清空
                        $up['am_status']=$status[0];
                        $up['am_leave_status']=0;
                        $up['am_type']=0;
                    }
                    if($val['pm']){
                        $up['pm_status']=$status[1];
                        $up['pm_leave_status']=0;
                        $up['pm_type']=0;
                    }*/
                }
                $this->daily_bll->update_kq_status($up,array('user_id'=>$leave_data['user_id'],'dateline'=>$k));
            }
        }
        return true;

    }

    //请假类型汉字转成数字
    public function format_leave_type($type){
        if(is_numeric($type)){
            return $type;
        }
        $leave_type = APF::get_instance()->get_config('leave_type');
        $key = array_search($type,$leave_type);
        return $key;
    }
    //请假状态汉字转成数字
    public function format_leave_status($status){
        if(is_numeric($status)){
            return $status;
        }
        $leave_status = APF::get_instance()->get_config('leave_status');
        $key = array_search($status,$leave_status);
        return $key;
    }
    /**
     * 得到员工一天的打卡记录，没有则记为上下午缺勤
     * @param int $uid
     * @param int $day
     * @return multitype:number
     */
    public function get_one_accessday($uid,$day){
        $bll = new Bll_Attendance_AccessDayRecord();
        $acdata = $bll->get_user_day($uid,$day);
        if(!empty($acdata)){
            $acdata['status'] = $bll->check_day_work_status($day,$acdata[0]['start_work'],$acdata[0]['end_work']);
        }else{
            $acdata['status'] = array(Const_LeaveType::KQ_AM_STATUS_3,Const_LeaveType::KQ_PM_STATUS_4);
        }
        return $acdata;
    }

    private function build_curr_insert($leave_data,$val,$day){
        $data= array(
                    'user_id'=>$leave_data['user_id'],
                    'dateline'=>$day,
                    'leave_id'=>$leave_data['id'],
                    'leave_type'=>$leave_data['type'],
                    'leave_status'=>$leave_data['status'],
                    'leave_apm'=>$val['apm'],
                    'leave_time'=>$val['am']+$val['pm']
                );
        if($leave_data['type']==Const_LeaveType::LEAVE_TYPE_GONGCHU){
            $data['leave_gc_time'] = $val['gc_time'];
            $data['leave_time'] = $val['business_travel'];
        }
        return $data;
    }
    private function build_prev_insert(){

    }
    private function build_next_insert(){

    }

    /**
     *  迟到处理分 未审核 已批准 已撤消 已驳回
     * @param int $leave_id
     * @param int $op_status
     * @param int $daylist
     * @return boolean
     */
    public function update_come_late($leave_data,$op_status=1){
        $daylist = $leave_data['daylist'];
        $day = date('Ymd',$leave_data['start_time']);

        switch ($op_status){
            case Const_LeaveType::LEAVE_STATUS_AUDIT:
                $in=$this->build_curr_insert($leave_data,$daylist[$day],$day);
                $this->daily_bll->insert_record_curr($in);
                $up = array(
                    //'am_type'=>$leave_data['type'],
                    'am_leave_status'=>$leave_data['status'],
                );
                $this->daily_bll->update_kq_status($up, array('user_id'=>$leave_data['user_id'],'dateline'=>$day));
            break;
            case Const_LeaveType::LEAVE_STATUS_PASS:
                $up = array(
                    'am_leave_status'=>Const_LeaveType::LEAVE_STATUS_PASS,
                    'am_status'=>Const_LeaveType::KQ_AM_STATUS_5,
                    'is_later'=>0,
                );
                //需要特殊处理,如果是忘记带卡，需要也需要修改上下午状态 id=11419
                if($leave_data['leave_reason']=='忘记带卡'){
                    $up['pm_status'] = Const_LeaveType::KQ_PM_STATUS_6;
                    $up['pm_leave_status'] = Const_LeaveType::LEAVE_STATUS_PASS;
                    $up['leave_early']=0;
                }
                $this->daily_bll->update_kq_status($up,array('user_id'=>$leave_data['user_id'],'dateline'=>$day));
                $this->daily_bll->update_record_curr(array('leave_status'=>Const_LeaveType::LEAVE_STATUS_PASS),array('leave_id'=>$leave_data['id']));
            break;
            case Const_LeaveType::LEAVE_STATUS_CANCEL:
            case Const_LeaveType::LEAVE_STATUS_OVERRULE:

                $up = array(
                    'am_leave_status'=>0,
                );
                //需要特殊处理,如果是忘记带卡，需要也需要修改上下午状态 id=11419
                if($leave_data['leave_reason']=='忘记带卡'){
                    $up['pm_leave_status'] = 0;
                }
                $this->daily_bll->update_kq_status($up,array('user_id'=>$leave_data['user_id'],'dateline'=>$day));
                $this->daily_bll->update_record_curr(array('leave_status'=>$op_status),array('leave_id'=>$leave_data['id']));
            break;
            case 100:// Job里跑历史数据时需要使用
                $in=$this->build_curr_insert($leave_data,$daylist[$day],$day);
                $this->daily_bll->insert_record_curr($in);
                if($leave_data['status']>1){
                    $this->update_come_late($leave_data,$leave_data['status']);//回调
                }
            break;
        }
        return true;
    }
    /**
     * 早退处理
     * @param int $leave_id
     * @param int $op_status
     */
    public function update_left_early($leave_data,$op_status=1){
        $daylist = $leave_data['daylist'];
        $day = date('Ymd',$leave_data['start_time']);
        switch ($op_status){
            case Const_LeaveType::LEAVE_STATUS_AUDIT:
                    $in=$this->build_curr_insert($leave_data,$daylist[$day],$day);
                    $this->daily_bll->insert_record_curr($in);
                    $up = array(
                           // 'pm_type'=>$leave_data['type'],
                            'pm_leave_status'=>$leave_data['status'],
                    );
                    $this->daily_bll->update_kq_status($up, array('user_id'=>$leave_data['user_id'],'dateline'=>$day));
                break;
            case Const_LeaveType::LEAVE_STATUS_PASS:
                    $this->daily_bll->update_kq_status(array('pm_leave_status'=>2,'pm_status'=>Const_LeaveType::KQ_PM_STATUS_6,'leave_early'=>0),array('user_id'=>$leave_data['user_id'],'dateline'=>$day));
                    $this->daily_bll->update_record_curr(array('leave_status'=>Const_LeaveType::LEAVE_STATUS_PASS),array('leave_id'=>$leave_data['id']));
                break;
            case Const_LeaveType::LEAVE_STATUS_CANCEL:
            case Const_LeaveType::LEAVE_STATUS_OVERRULE:

                $this->daily_bll->update_kq_status(array('pm_leave_status'=>0),array('user_id'=>$leave_data['user_id'],'dateline'=>$day));
                $this->daily_bll->update_record_curr(array('leave_status'=>$op_status),array('leave_id'=>$leave_data['id']));
                break;
            case 100:// Job里跑历史数据时需要使用
                $this->update_left_early($leave_data,1);
                if($leave_data['status']>1){
                    $this->update_left_early($leave_data,$leave_data['status']);
                }
            break;
        }
        return true;
    }
    /**
     * 公出处理
     * @param int $leave_id
     * @param int $op_status
     */
    public function update_business_travel($leave_data,$op_status=1){
        $result = $this->split_leave_days($leave_data);
        $daylist = $leave_data['daylist'];
        $day = date('Ymd',$leave_data['start_time']);

        switch ($op_status){
            case Const_LeaveType::LEAVE_STATUS_AUDIT:
                if($day > date('Ymd')){#如果是今天之后的数据需要写入user_day_status
                    $this->daily_bll->insert_record_kq_status($leave_data['user_id'],$day);
                }
                $daylist[$day]['gc_time']=date('H:i',$leave_data['start_time']).'-'.date('H:i',$leave_data['end_time']);
                if(empty($result['prev'])){//不是上月未清的数据
                    $in = $this->build_curr_insert($leave_data,$daylist[$day],$day);
                    $this->daily_bll->insert_record_curr($in);
                }else{
                    $curr_status = $this->get_one_accessday($leave_data['user_id'],$day);
                    $last_status = $this->travel_to_kq_status($leave_data['user_id'],$day,$leave_data['start_time'],$leave_data['end_time']);
                    $am=$pm=0;
                    if($curr_status['status'][0]==3){
                        if($last_status[0]==5){
                            $am=1;
                        }
                    }
                    if($curr_status['status'][1]==4){//TODO迟到、早退、没有还原
                        if($last_status[1]==6){
                            $pm=1;
                        }
                    }
                    if($am && $pm){
                        $daylist[$day]['apm']=3;
                    }elseif($am && !$pm){
                        $daylist[$day]['apm']=1;
                    }else{
                        $daylist[$day]['apm']=2;
                    }
                    $in=array(
                            'user_id'=>$leave_data['user_id'],
                            'dateline'=>$day,
                            'leave_id'=>$leave_data['id'],
                            'leave_type'=>$leave_data['type'],
                            'leave_status'=>$leave_data['status'],
                            'leave_apm'=>$daylist[$day]['apm'],//1,2,3
                            'leave_days'=>$daylist[$day]['am']+$daylist[$day]['pm']
                    );
                    //上月未清的数据,不更新状态,前台显示的通过取到上月未清的数据进行展示
                    $this->daily_bll->insert_record_prev($in);
                }
                break;
            case Const_LeaveType::LEAVE_STATUS_PASS:
                if(!empty($result['prev'])){ //上月未清表,不修改当天状态
                    $this->daily_bll->update_record_prev(array('leave_status'=>Const_LeaveType::LEAVE_STATUS_PASS),array('leave_id'=>$leave_data['id']));
                    break;
                }
                $oneday=$this->daily_bll->find_kq_by_dateline($leave_data['user_id'],$day);
                $last_status = $this->travel_to_kq_status($leave_data['user_id'],$day,$leave_data['start_time'],$leave_data['end_time']);
                $up = array();
                if($oneday['am_status']!=5){
                    $up['am_status']=$last_status[0];
                    if($last_status[0]==5){
                        $up['am_leave_status']=Const_LeaveType::LEAVE_STATUS_PASS;
                    }
                }
                if($oneday['pm_status']!=6){
                    $up['pm_status']=$last_status[1];
                    if($last_status[1]==6){
                        $up['pm_leave_status']=Const_LeaveType::LEAVE_STATUS_PASS;
                    }
                }
                //这个员工是否为特殊班别,且还在有效期
                if($leave_data['hm_date'] && $leave_data['hm_date']>=$day){
                    $up['am_status'] = 5 ;
                    $up['pm_status'] = 6 ;
                    $up['am_leave_status'] = $up['pm_leave_status'] = Const_LeaveType::LEAVE_STATUS_PASS;
                }
                $this->daily_bll->update_kq_status($up,array('user_id'=>$leave_data['user_id'],'dateline'=>$day));
                $this->daily_bll->update_record_curr(array('leave_status'=>Const_LeaveType::LEAVE_STATUS_PASS),array('leave_id'=>$leave_data['id']));

                break;
            case Const_LeaveType::LEAVE_STATUS_CANCEL:
            case Const_LeaveType::LEAVE_STATUS_OVERRULE:
                if(!empty($result['prev'])){
                    //上月未清表
                    $this->daily_bll->update_record_prev(array('leave_status'=>$op_status),array('leave_id'=>$leave_data['id']));
                    foreach($daylist as $day=>$val){
                        $status = $this->get_one_accessday($leave_data['user_id'],$day);
                        $up = $this->back_kq_status($leave_data['user_id'],$day,$status['status']);
                        $this->daily_bll->update_kq_status($up,array('user_id'=>$leave_data['user_id'],'dateline'=>$day));
                    }
                    break;
                }
                $this->daily_bll->update_record_curr(array('leave_status'=>$op_status),array('leave_id'=>$leave_data['id']));
                break;
            case 100:// Job里跑历史数据时需要使用
                $this->update_business_travel($leave_data,1);
                if($leave_data['status']>1){
                    $this->update_business_travel($leave_data,$leave_data['status']);
                }
            break;
        }
        return true;
    }

    /**
     *  计算公出是否能覆盖考勤状态
     * @param int $uid
     * @param int $day
     * @param int $s_time
     * @param int $e_time
     */
    public function travel_to_kq_status($uid,$day,$s_time,$e_time){
        $bll = new Bll_Attendance_AccessDayRecord();
        $acdata = $bll->get_user_day($uid,$day);
        $start_work_s = date('Hi',$s_time);
        $end_work_s = date('Hi',$e_time);
        $status=array(5,6);
        if($start_work_s<=900 && $end_work_s>=1800){
            return $status;
        }
        if(empty($acdata)){//没有打卡记录
            $start_work = $start_work_s;
            $end_work = $end_work_s;
        }else{
            $start_work = $acdata[0]['start_work'];
            $end_work = $acdata[0]['end_work'];
        }
        $start_work = min($start_work_s,$start_work);//公出相当于打卡
        $end_work = max($end_work,$end_work_s);
        $status = $bll->check_day_work_status($day,$start_work,$end_work);
        return $status;//TODO如果这一天是特殊工作日时间需要特殊处理,这里直接返回最终状态
        if($status[0] ==5 && $status[1] ==6){
            return $status;
        }
        if($status[0]!=5){
            if($start_work<=900){
                $status[0]=5;
            }
        }
        if($status[1]!=6){
            if($start_work <=1400 && $end_work>=1800){
                $status[1]=6;
            }
        }
        return $status;
    }
    /**
     * 出差的记录需要写入 user_day_status表
     */
    public function update_business_trip($leave_data,$op_status){
        $daylist = $leave_data['daylist'];
        $today = date('Ymd');
        switch ($op_status){
            case Const_LeaveType::LEAVE_STATUS_AUDIT:
                foreach($daylist as $day=>$val){
                    if($day>$today){#如果是今天之后的数据需要写入user_day_status
                        $this->daily_bll->insert_record_kq_status($leave_data['user_id'],$day);
                    }
                    $in=$this->build_curr_insert($leave_data,$val,$day);
                    $this->daily_bll->insert_record_curr($in);

                    $up = array(
                            'am_type'=>$leave_data['type'],
                            'am_leave_status'=>$leave_data['status'],
                            'pm_type'=>$leave_data['type'],
                            'pm_leave_status'=>$leave_data['status'],
                            'days'=>$val['am']+$val['pm']
                            );
                    if($val['apm']==1){
                        unset($up['pm_type']);
                        unset($up['pm_leave_status']);
                    }elseif($val['apm']==2){
                        unset($up['am_type']);
                        unset($up['am_leave_status']);
                    }
                    $this->daily_bll->update_kq_status($up, array('user_id'=>$leave_data['user_id'],'dateline'=>$day));
                }

                break;
            case Const_LeaveType::LEAVE_STATUS_PASS:
                $this->daily_bll->update_record_curr(array('leave_status'=>Const_LeaveType::LEAVE_STATUS_PASS),array('leave_id'=>$leave_data['id']));
                foreach($daylist as $day=>$val){
                    $up = array(
                        'am_status'=>Const_LeaveType::KQ_AM_STATUS_5,
                        'am_leave_status'=>$leave_data['status'],
                        'pm_status'=>Const_LeaveType::KQ_PM_STATUS_6,
                        'pm_leave_status'=>$leave_data['status'],
                    );
                    if($val['apm']==1){
                        $up['is_later'] = 0;
                        unset($up['pm_status']);
                        unset($up['pm_leave_status']);
                    }elseif($val['apm']==2){
                        unset($up['am_status']);
                        unset($up['am_leave_status']);
                        $up['leave_early'] = 0;
                    }
                    //这个员工是否为特殊班别,且还在有效期
                    if($leave_data['hm_date'] && $leave_data['hm_date']>=$day){
                        $up['am_status'] = 5 ;
                        $up['pm_status'] = 6 ;
                    }
                    $this->daily_bll->update_kq_status($up, array('user_id'=>$leave_data['user_id'],'dateline'=>$day));
                }
                break;
            case Const_LeaveType::LEAVE_STATUS_CANCEL:
            case Const_LeaveType::LEAVE_STATUS_OVERRULE:
                foreach($daylist as $day=>$val){
                    $oneday=$this->daily_bll->find_kq_by_dateline($leave_data['user_id'],$day);
                    if(!empty($oneday)){
                        $up = array('days'=>$oneday['days']);
                        if($val['apm']==1){
                            $up['days'] -=0.5;
                            $up['pm_type']=0;
                            $up['pm_leave_status']=0;
                        }elseif($val['apm']==2){
                            $up['days'] -=0.5;
                            $up['am_type']=0;
                            $up['am_leave_status']=0;
                        }else{
                            $up['days'] =0;
                        }
                        $this->daily_bll->update_kq_status($up, array('user_id'=>$leave_data['user_id'],'dateline'=>$day));
                    }
                }
                $this->daily_bll->update_record_curr(array('leave_status'=>$op_status),array('leave_id'=>$leave_data['id']));
                break;
            case 100:// Job里跑历史数据时需要使用
                foreach($daylist as $day=>$val){
                    $in=$this->build_curr_insert($leave_data,$val,$day);
                        $up = array(
                                'am_type'=>$leave_data['type'],
                                'am_leave_status'=>$leave_data['status'],
                                'pm_type'=>$leave_data['type'],
                                'pm_leave_status'=>$leave_data['status'],
                                'days'=>$val['am']+$val['pm']
                                );
                    if($val['apm']==1){
                        unset($up['pm_type']);
                        unset($up['pm_leave_status']);
                    }elseif($val['apm']==2){
                        unset($up['am_type']);
                        unset($up['am_leave_status']);
                    }
                    if($leave_data['status']==2){//只有批准才会操作
                        if($val['apm']==1){
                            $up['am_status']=5;
                        }elseif($val['apm']==2){
                            $up['pm_status']=6;
                        }else{
                            $up['am_status']=5;
                            $up['pm_status']=6;
                        }
                    }
                    $this->daily_bll->update_kq_status($up, array('user_id'=>$leave_data['user_id'],'dateline'=>$day));
                    $this->daily_bll->insert_record_curr($in);
                }
            break;
        }
        return true;
    }
    /**
     * 缺勤需要区分是否为上月未清
     */
    public function update_absend($leave_data,$op_status){
        return true;
    }
    /**
     * 请假这里的记录分为两种,1-结算日的和上月未清数据
     * 产检需要特殊处理，每月一次，每次一天TODO
     * @param int $leave_id
     * @param int $op_status
     */
    public function update_common_leave($leave_data,$op_status=1){
        $daylist = $leave_data['daylist'];
        $today = date('Ymd');
        //需要将假日拆分为三种数据
        $result = $this->split_leave_days($leave_data);
        switch ($op_status){
            case Const_LeaveType::LEAVE_STATUS_AUDIT://TODO分三个类别处理的，代码很长哇
                /*foreach($daylist as $day=>$val){
                    if($day>$today){#如果是今天之后的数据需要写入user_day_status
                        $this->daily_bll->insert_record_kq_status($leave_data['user_id'],$day);
                    }
                }*/
                $this->daily_bll->insert_record_kq_status_multi($daylist,$leave_data['user_id']);
                if(!empty($result['curr'])){
                    foreach($result['curr'] as $day=> $val){
                        $in=$this->build_curr_insert($leave_data,$val,$day);
                        $up = array(
                                'am_type'=>$leave_data['type'],
                                'am_leave_status'=>$leave_data['status'],
                                'pm_type'=>$leave_data['type'],
                                'pm_leave_status'=>$leave_data['status'],
                                'days'=>$val['am']+$val['pm']
                        );
                        if($val['apm']==1){
                            unset($up['pm_type']);
                            unset($up['pm_leave_status']);
                        }elseif($val['apm']==2){
                            unset($up['am_type']);
                            unset($up['am_leave_status']);
                        }
                        if($leave_data['type']==Const_LeaveType::LEAVE_TYPE_QUEQIN){//如果类型是缺勤,强制不存到kq_status表中
                            $up['am_type']=$up['pm_tpye']=0;
                            //$up=array('days'=>$val['am']+$val['pm']);
                        }
                        $this->daily_bll->update_kq_status($up, array('user_id'=>$leave_data['user_id'],'dateline'=>$day));
                        $this->daily_bll->insert_record_curr($in);
                    }
                }
                if(!empty($result['prev'])){
                    foreach($result['prev'] as $day=> $val){
                        $in=array(
                                'user_id'=>$leave_data['user_id'],
                                'dateline'=>$day,
                                'leave_id'=>$leave_data['id'],
                                'leave_type'=>$leave_data['type'],
                                'leave_status'=>$leave_data['status'],
                                'leave_apm'=>$val['apm'],//1,2,3
                                'leave_days'=>$val['am']+$val['pm']
                        );
                        //上月未清的数据,不更新状态,前台显示的通过取到上月未清的数据进行展示
                        $this->daily_bll->insert_record_prev($in);
                    }
                }
                if(!empty($result['next'])){
                    foreach($result['next'] as $day=> $val){
                        $insert = array(
                            'user_id'=>$leave_data['user_id'],
                            'dateline'=>$day,
                            'am_type'=>$leave_data['type'],
                            'am_leave_id'=>$leave_data['id'],
                            'am_leave_status'=>$leave_data['status'],
                            'pm_type'=>$leave_data['type'],
                            'pm_leave_id'=>$leave_data['id'],
                            'pm_leave_status'=>$leave_data['status'],
                            );
                        $up = array(
                                'am_type'=>$leave_data['type'],
                                'am_leave_status'=>$leave_data['status'],
                                'pm_type'=>$leave_data['type'],
                                'pm_leave_status'=>$leave_data['status'],
                                'days'=>$val['am']+$val['pm']
                        );
                        if($val['apm']==1){
                            unset($insert['pm_type']);
                            unset($insert['pm_leave_id']);
                            unset($insert['pm_leave_status']);

                            unset($up['pm_type']);
                            unset($up['pm_leave_status']);
                        }elseif($val['apm']==2){
                            unset($insert['am_type']);
                            unset($insert['am_leave_id']);
                            unset($insert['am_leave_status']);

                            unset($up['am_type']);
                            unset($up['am_leave_status']);
                        }
                        if($leave_data['type']==Const_LeaveType::LEAVE_TYPE_QUEQIN){//如果类型是缺勤,强制不存到kq_status表中
                            //$up=array('days'=>$val['am']+$val['pm']);
                            $up['am_type']=$up['pm_type']=0;
                        }
                        $this->daily_bll->update_kq_status($up, array('user_id'=>$leave_data['user_id'],'dateline'=>$day));
                        $this->daily_bll->insert_record_next($insert);
                    }
                }
                $result=array();//reset...
                break;
            case Const_LeaveType::LEAVE_STATUS_PASS:#这种假期记录和天数都存进去了。只用修改状态就可以
                //这里分两次分别对上午的状态进行改变
                $am_where=array('am_leave_id'=>$leave_data['id']);
                $pm_where=array('pm_leave_id'=>$leave_data['id']);
                $this->daily_bll->update_record_next(array('am_leave_status'=>Const_LeaveType::LEAVE_STATUS_PASS),$am_where);
                $this->daily_bll->update_record_next(array('pm_leave_status'=>Const_LeaveType::LEAVE_STATUS_PASS),$pm_where);

                $this->daily_bll->update_record_curr(array('leave_status'=>Const_LeaveType::LEAVE_STATUS_PASS),array('leave_id'=>$leave_data['id']));
                //上月未清表
                $this->daily_bll->update_record_prev(array('leave_status'=>Const_LeaveType::LEAVE_STATUS_PASS),array('leave_id'=>$leave_data['id']));

                foreach($daylist as $day=>$val){
                    if(isset($result['prev'][$day])){//上月未清计算,属于上月未清范围的都不修改user_day_status状态
                        continue;
                    }
                    $up = array(
                            'am_status'=>Const_LeaveType::KQ_AM_STATUS_5,
                            'am_leave_status'=>$leave_data['status'],
                            'pm_status'=>Const_LeaveType::KQ_PM_STATUS_6,
                            'pm_leave_status'=>$leave_data['status'],
                            'days'=>$val['am']+$val['pm']
                    );
                    //如果是缺勤也不需要存到类型入库,只需要变更状态,缺勤
                    if($leave_data['type']==Const_LeaveType::LEAVE_TYPE_QUEQIN){
                        if($val['apm']==1){
                            unset($up['pm_leave_status']);
                            $up['is_later'] = 0;
                        }elseif($val['apm']==2){
                            unset($up['am_leave_status']);
                            $up['leave_early'] = 0;
                        }
                        unset($up['days']);
                    }else{
                        if($val['apm']==1){
                            $up['is_later'] = 0;
                            unset($up['pm_status']);
                            unset($up['pm_leave_status']);
                        }elseif($val['apm']==2){
                            unset($up['am_status']);
                            unset($up['am_leave_status']);
                            $up['leave_early'] = 0;
                        }
                    }
                    //这个员工是否为特殊班别,且还在有效期
                    if($leave_data['hm_date'] && $leave_data['hm_date']>=$day){
                        $up['am_status'] = 5 ;
                        $up['pm_status'] = 6 ;
                    }
                    $this->daily_bll->update_kq_status($up, array('user_id'=>$leave_data['user_id'],'dateline'=>$day));
                }
                break;
            case Const_LeaveType::LEAVE_STATUS_CANCEL:
            case Const_LeaveType::LEAVE_STATUS_OVERRULE:

                $am_where['am_leave_id'] = $leave_data['id'];
                $pm_where['pm_leave_id'] = $leave_data['id'];
                //这里分两次分别对上午的状态进行改变
                $this->daily_bll->update_record_next(array('am_leave_status'=>$op_status),$am_where);
                $this->daily_bll->update_record_next(array('pm_leave_status'=>$op_status),$pm_where);

                $this->daily_bll->update_record_curr(array('leave_status'=>$op_status),array('leave_id'=>$leave_data['id']));
                //上月未清表
                $this->daily_bll->update_record_prev(array('leave_status'=>$op_status),array('leave_id'=>$leave_data['id']));
                foreach($daylist as $day=>$val){
                    $status = $this->get_one_accessday($leave_data['user_id'],$day);
                    $up = $this->back_kq_status($leave_data['user_id'],$day,$status['status']);
                    $this->daily_bll->update_kq_status($up,array('user_id'=>$leave_data['user_id'],'dateline'=>$day));
                }
            break;
            case 100:
                $this->update_common_leave($leave_data,1);//这里用了一次递归写入数据
                //#如果是审批通过的去修改下状态
                if($leave_data['status']>1){
                    $this->update_common_leave($leave_data,$leave_data['status']);
                }
                break;
        }
        return true;
    }

    //通过结算日得到相关几个时间
    private function time_markup($day){
        $unix_time = strtotime($day);
        $year = date('Y',$unix_time);
        $month = date('m',$unix_time);

        return array(
                'curr_month_start_day'=>date('Ymd',mktime(0,0,0,$month,01,$year)),//上月第一天 20120701
                'curr_month_end_day'=>date('Ymd',mktime(0,0,0,$month+1,0,$year)),//当前最后天20120731
                'next_month_start_day'=>date('Ymd',mktime(0,0,0,$month+1,01,$year)),//下月第一天 20120801
                'next_month_end_day'=>date('Ymd',mktime(0,0,0,$month+2,0,$year)) //下有最后一天20120831
                );
    }
    /**
     * 格式化一次申请所有天数的每日细节
     * @param int $leave_start
     * @param int $leave_end
     * @param int $type
     * @return multitype:multitype:number string  multitype:number unknown
     */
    public function format_leave_days($leave_start,$leave_end,$type){
        $dateline = date('Ymd',$leave_start);
        $result = array();
        switch ($type){
            case Const_LeaveType::LEAVE_TYPE_CHIDAO:#'迟到'
                $result[$dateline]=array(
                'am'=>0,
                'pm'=>0,
                'apm'=>1,
                'com_late'=>1,
                'left_early'=>0,
                'business_travel'=>0,
                'type'=>'迟到'
                        );
            break;
            case Const_LeaveType::LEAVE_TYPE_ZAOTUI:#'早退'
                $result[$dateline]=array(
                'am'=>0,
                'pm'=>0,
                'apm'=>2,
                'com_late'=>0,
                'left_early'=>1,
                'business_travel'=>0,
                'type'=>'早退'
                        );
            break;
            case Const_LeaveType::LEAVE_TYPE_GONGCHU:#'公出'
                $cnt =  ($leave_end-$leave_start)/3600;
                $c_end_h =date('H',$leave_start);
                if($c_end_h==0 || $c_end_h==23 ){
                    $cnt+=1;
                }
                $result[$dateline]=array(
                        'am'=>0,
                        'pm'=>0,
                        'apm'=>0,
                        'com_late'=>0,
                        'left_early'=>0,
                        'business_travel'=>$cnt,
                        'type'=>'公出'
                );
            break;
            default:#缺勤 出差 各种假
                $start_h = date('H',$leave_start);
                $end_h = date('H',$leave_end);
                $end_day = date('Ymd',$leave_end);
                $j=1;
                for ($i=$leave_start;$i<=$leave_end;$i+=86400){#拆分为上下午
                    //这里计算补假后应该使用的天数
                    $date = date('Ymd',$i);
                    $data = array(
                            'am'=>0.5,
                            'pm'=>0.5,
                            'apm'=>3,
                            'com_late'=>0,
                            'left_early'=>0,
                            'business_travel'=>0,
                            'type'=>$type
                    );
                    if($j==1){//第一天
                        if($start_h==14){
                            $data['am']=0;
                            $data['apm']=2;
                        }elseif($start_h==9 && $end_h==14 && $date==$end_day){
                            $data['pm']=0;
                            $data['apm']=1;
                        }
                    }elseif($j > 1 && $date==$end_day){//最后一天
                        if($end_h==14){
                            $data['pm']=0;
                            $data['apm']=1;
                        }
                    }
                    $result[$date] = $data;
                    $j++;
                }
                break;
        }
        return $result;
    }
    //特殊班别的处理
    private function check_special_work_type(){

    }
}
